Copyright © Wei MEI, MLMS™—all rights reserved. 🀤
Error Handling¶
Error handling in Python is managed through the use of try/except/finally
Python has numerous built-in exceptions. When creating except blocks, they need to be created from most specific to most generic according to the hierarchy.
Date data types¶
1 2 3 4 | x = 42
y = 206
if x == y
print('Success')
|
1 2 3 4 5 6 7 8 9 10 11 | x = 42
y = 0
try:
print(x / y)
except ZeroDivisionError as e:
# Optionally, log e somewhere
print('Sorry, something went wrong')
except:
print('Something really went wrong')
finally:
print('This always runs on success or failure')
|
Demo: dates¶
1 2 3 4 | x = 206
y = 42
if x < y:
print(str(x) + ' is greater than ' + str(y))
|